import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:overload_guard/app_theme.dart';
import 'package:overload_guard/pages/sign_in_page.dart';
import 'package:overload_guard/services/api_service.dart';
import 'package:overload_guard/services/db_service.dart';
import 'package:overload_guard/widgets/buttons.dart';
import 'package:overload_guard/widgets/dialogs.dart';

class ChangePhonePage extends StatefulWidget {
  @override
  State<ChangePhonePage> createState() => _ChangePhonePageState();
}

class _ChangePhonePageState extends State<ChangePhonePage> {
  TextEditingController oldController = TextEditingController();
  TextEditingController newController = TextEditingController();
  TextEditingController new2Controller = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
          title: Text("修改联系电话", style: TextStyle(color: Colors.white)),
          backgroundColor: AppTheme.primaryColor,
          centerTitle: true,
          leading: IconButton(
            icon: Icon(
              Icons.arrow_back,
              color: Colors.white,
            ),
            onPressed: () {
              Get.back();
            },
          )),
      body: Padding(
        padding: EdgeInsets.only(left: 30, right: 30),
        child: ListView(
          children: [
            Padding(padding: EdgeInsets.only(top: 10)),
            TextField(
              controller: oldController,
              decoration: InputDecoration(
                  enabledBorder: OutlineInputBorder(
                      borderSide: BorderSide(color: Colors.grey)),
                  focusedBorder: OutlineInputBorder(
                      borderSide: BorderSide(color: AppTheme.primaryColor)),
                  hintText: "请输入原电话号码",
                  labelText: "原电话号码"),
            ),
            Padding(padding: EdgeInsets.only(top: 10)),
            TextField(
              controller: newController,
              decoration: InputDecoration(
                  enabledBorder: OutlineInputBorder(
                      borderSide: BorderSide(color: Colors.grey)),
                  focusedBorder: OutlineInputBorder(
                      borderSide: BorderSide(color: AppTheme.primaryColor)),
                  hintText: "请输入新电话号码",
                  labelText: "新电话号码"),
            ),
            Padding(padding: EdgeInsets.only(top: 10)),
            TextField(
              controller: new2Controller,
              decoration: InputDecoration(
                  enabledBorder: OutlineInputBorder(
                      borderSide: BorderSide(color: Colors.grey)),
                  focusedBorder: OutlineInputBorder(
                      borderSide: BorderSide(color: AppTheme.primaryColor)),
                  hintText: "请再次输入新电话号码",
                  labelText: "确认电话号码"),
            ),
            Padding(padding: EdgeInsets.only(top: 10)),
            mainSubmitButton("立即修改", () async {
              var result = await APIService.userUpdatePhone(
                  phone: await DBService.getData(DBKey.userPhone),
                  newWord: newController.text,
                  old: oldController.text,
                  repeat: new2Controller.text);

              if (result != null) {
                print('成功');
                Dialogs.showSuccess(title: '成功', body: '修改成功，请重新登陆');

                await DBService.saveData(DBKey.userSignInStatus, false);
                Get.offAll(SignInPage());
              }
            }, context, AppTheme.primaryColor),
          ],
        ),
      ),
    );
  }
}
